package edu.hawaii.solardecathlon.page.electricity;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
/**
 * Handles the dialog for updating the alarm limit.
 * 
 * @author Bret Ikehara
 */
public class ElectricityModalPage extends WebPage {

  /**
   * Serial ID.
   */
  private static final long serialVersionUID = -5197635570678522684L;

  /**
   * Creates the modal window for the Electricity page.
   * 
   * @param modal final ElectricityModalWindow
   */
  public ElectricityModalPage(final ElectricityModalWindow modal) {
    //TODO save the value of the electricity limit in a database.
    //TODO form validation and error message.

    Form<String> form = new Form<String>("ElecLimitUpdateForm");
    form.add(new Label("ElecApp", modal.getElec().getAppliance()));
    form.add(new Label("ElecLimitOld", String.valueOf(modal.getElec().getLimit())));
    form.add(new Label("ElecLimitNew"));
    form.add(new AjaxButton("Submit") {

      /**
       * Serial ID.
       */
      private static final long serialVersionUID = -2139897746860212118L;

      /**
       * Updates the Electrical Appliance's limits.
       */
      @Override
      protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
        modal.close(target);
        
        // TODO Save data into database.
      }
    });
    
    form.add(new AjaxButton("Cancel") {

      /**
       * Serial ID.
       */
      private static final long serialVersionUID = 7449284001039562738L;

      /**
       * Closes the dialog.
       * 
       * @param form Form<?>
       * @param target AjaxRequestTarger
       */
      @Override
      protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
        modal.close(target);    
      }
      
    });
    
    // Add components to the modal window.
    add(form);
  }  
}
